﻿using System;
using System.Collections.Generic;
using System.Text;
using TK3DSim.Action;
using TK3DSim;
using TK3DSim.Other;
using TerraExplorerX;
using TK3DSim.Mil;
using System.Windows.Forms;

namespace Tpro6Container.Class.ObjectClass
{
    public class RunOneStep
    {
        int num;

        List<ITerraExplorerObject6> armyobjects = null;
        List<Army> allarmys = null;
        double timelong = 0;
        DateTime now;
        TK3DSim.Action.Action oneStep;

        List<Vector2D> allway = new List<Vector2D>();
        List<double> evlelong = new List<double>();
        double longthforlu = 0;
        double nowlong;
        public RunOneStep(TK3DSim.Action.Action oneStep)
        {
            nowlong = 0;
            allway = new List<Vector2D>();
            evlelong = new List<double>();
            longthforlu = 0;
            now = DateTime.Now;
            timelong = oneStep.FightTimeLength;
            allarmys = new List<Army>();
            armyobjects =  new List<ITerraExplorerObject6> ();
            this.oneStep = oneStep;
            init(oneStep);

            //System.Windows.Forms.Timer daytimer = new System.Windows.Forms.Timer();
            //daytimer.Interval = 40;
            //daytimer.Tick += new EventHandler(daytimer_Tick);
            //daytimer.Start();
        }

        public bool RunOneTime ( )
        {
            if (oneStep.ActionKind == 0)
            {
                double onele = Global.Instance.GlobalArmys[oneStep.ActionArmyID].MoveSpeed / 3.6 / 25 * Mainform.TimeScal;
                nowlong = nowlong + onele;
                if(nowlong<=longthforlu)
                    Run(nowlong, onele);
                else if (nowlong > longthforlu && (nowlong - onele) < longthforlu)
                {
                    Run(nowlong, longthforlu- (nowlong - onele));
                }

                DateTime now1 = DateTime.Now;
                TimeSpan times = now1 - now;
                if (timelong > 0)
                {
                    if (times.TotalMinutes * Mainform.TimeScal > timelong)
                    {
                        foreach (Army arm in allarmys)
                        {
                            int fireid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + arm.ArmyName + "\\开火");
                            int tegetid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + arm.ArmyName + "\\目标");
                            int modleid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + arm.ArmyName + "\\模型");
                            if (fireid>0)
                            GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(fireid, false);
                            if (tegetid>0)
                            GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(tegetid, false);
                            if (modleid > 0)
                            {
                                int modle = GlobalObjects.Instance.World3D.ProjectTree.GetNextItem(modleid, ItemCode.CHILD);
                                while (modle > 0)
                                {
                                    GlobalObjects.Instance.World3D.ProjectTree.GetObject(modle);

                                    modle = GlobalObjects.Instance.World3D.ProjectTree.GetNextItem(modle, ItemCode.NEXT);
                                }
                            }
                        }
                        return true;
                    }
                }
            }
            else
            {
                return true;
            }
            return false;
        }

        private void Run(double nowlong,double once)
        {

            if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.路径)
            {
                double longthforone =0 ;
                int veccount = 0;
                for (int i = 0; i < evlelong.Count; i++)
                {
                    longthforone = longthforone + evlelong[i];
                    if (longthforone > nowlong)
                    {
                        veccount = i;
                    }
                }
                Vector2D nowv = allway[veccount];
                nowv.Normalize();
                double an = Vector2D.AngleBetween(new Vector2D(0,1),nowv)/Math.PI*180;
                for (int armmnum = 0; armmnum < armyobjects.Count; armmnum++)
                {

                    ITerrainModel6 imodel = armyobjects[armmnum] as ITerrainModel6;
                    if (imodel == null)
                    {
                        ITerrainLabel6 ileble = armyobjects[armmnum] as ITerrainLabel6;

                        if (ileble != null)
                        {
                            ICoord2D mm = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(ileble.Position.X, ileble.Position.Y
                                , nowv.X * once,
                                nowv.Y *once);
                            ileble.Position.X = mm.X;
                            ileble.Position.Y = mm.Y;

                        }
                    }
                    else
                    {
                        ICoord2D mm = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(imodel.Position.X, imodel.Position.Y
                            , nowv.X * once,
                            nowv.Y * once);
                       //imodel.ModelType;
                        imodel.Position.X = mm.X;
                        imodel.Position.Y = mm.Y;
                        imodel.Position.Yaw = an;
                    }

                }
            }


        }
        // List<>
        private void init(TK3DSim.Action.Action oneStep)
        {

            List<int> ids = new List<int>();
            if (oneStep.ActionKind == 0)
            {
                Army army = Global.Instance.GlobalArmys[oneStep.ActionArmyID];
                string equid = Global.Instance.GlobalArmys[oneStep.ActionArmyID].EquipmentID;
                Armament equipmen;
                if (equid != "")
                    equipmen = Global.Instance.GlobalArmaments[equid];
                allarmys.Add(army);
                findallarmy(army);
                armyobjects =  new List<ITerraExplorerObject6> ();
                foreach (Army onearmy in allarmys)
                {
                    int ifidii = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\部队标签");
                    if (ifidii > 0)
                    {
                        ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(ifidii) as ITerraExplorerObject6;
                        armyobjects.Add(armyobject);
                    }
                    else
                    {
                        string equid1 = onearmy.EquipmentID;
                        Armament equipmen1 = null, equipmen2 = null, equipmen3 = null, equipmen4 = null, equipmen5 = null,
                            equipmen6 = null, equipmen7 = null, equipmen8 = null, equipmen9 = null, equipmen10 = null;
                        if (equid1 != "")
                        {
                            equipmen1 = Global.Instance.GlobalArmaments[equid1];
                        }
                        string equid2 = onearmy.EquipmentID1;
                        if (equid2 != "")
                        {
                            equipmen2 = Global.Instance.GlobalArmaments[equid2];
                        }
                        string equid3 = onearmy.EquipmentID2;
                        if (equid3 != "")
                        {
                            equipmen3 = Global.Instance.GlobalArmaments[equid3];
                        }
                        string equid4 = onearmy.EquipmentID3;
                        if (equid4 != "")
                        {
                            equipmen4 = Global.Instance.GlobalArmaments[equid4];
                        }
                        string equid5 = onearmy.EquipmentID4;
                        if (equid5 != "")
                        {
                            equipmen5 = Global.Instance.GlobalArmaments[equid5];
                        }
                        string equid6 = onearmy.EquipmentID5;
                        if (equid6 != "")
                        {
                            equipmen6 = Global.Instance.GlobalArmaments[equid6];
                        }

                        string equid7 = onearmy.EquipmentID6;
                        if (equid7 != "")
                        {
                            equipmen7 = Global.Instance.GlobalArmaments[equid7];
                        }

                        string equid8 = onearmy.EquipmentID7;
                        if (equid8 != "")
                        {
                            equipmen8 = Global.Instance.GlobalArmaments[equid8];
                        }

                        string equid9 = onearmy.EquipmentID8;
                        if (equid9 != "")
                        {
                            equipmen9 = Global.Instance.GlobalArmaments[equid9];
                        }
                        string equid10 = onearmy.EquipmentID9;
                        if (equid10 != "")
                        {
                            equipmen10 = Global.Instance.GlobalArmaments[equid10];
                        }
                        int armynum = onearmy.ArmamentCount;

                        for (int i = 0; i < armynum; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen1.Name + (i+1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount1; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen2.Name + (armynum + i+1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount2; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen3.Name + (onearmy.ArmamentCount1 + armynum + i+1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount3; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen4.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount4; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen5.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount5; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen6.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount6; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen7.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount7; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen8.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount8; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen9.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                        for (int i = 0; i < onearmy.ArmamentCount9; i++)
                        {
                            int eqid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + onearmy.ArmyName + "\\模型\\" + equipmen10.Name + (i + 1).ToString());
                            if (eqid > 0)
                            {
                                ITerraExplorerObject6 armyobject = GlobalObjects.Instance.World3D.ProjectTree.GetObject(eqid) as ITerraExplorerObject6;
                                armyobjects.Add(armyobject);
                            }
                        }
                    }
                }


                Vector2D vc = new Vector2D();
                Vector2D vd = new Vector2D();
                Location thetaget = new Location();
                Location thetagetrage = new Location();
                if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.地标)
                {
                    vc.X = oneStep.TargetLoaction.X - army.ArmyPost.X;
                    vc.Y = oneStep.TargetLoaction.Y - army.ArmyPost.Y;
                    vd = vc;
                    vc.Normalize();
                }

                else if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.范围)
                {
                    Range aa = new Range(oneStep.TargetRange);
                    double cx = (aa.LeftTopPoint.X + aa.RightBottom.X) / 2;
                    double cy = (aa.LeftTopPoint.Y + aa.RightBottom.Y) / 2;
                    thetaget.X = cx;
                    thetaget.X = cy;
                    thetagetrage.X = (aa.LeftTopPoint.X - aa.RightBottom.X) / 2;
                    thetagetrage.Y = (aa.LeftTopPoint.Y - aa.RightBottom.Y) / 2;

                    vc.X = cx - army.ArmyPost.X;
                    vc.Y = cy - army.ArmyPost.Y;
                    vd = vc;
                    vc.Normalize();
                }

                else if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.路径)
                {
                    int idsS = GlobalObjects.Instance.World3D.ProjectTree.FindItem("路径\\" + oneStep.AdditionalInfo);
                    if (idsS > 0)
                    {
                        //List<Vector2D> allway = new List<Vector2D>();
                        // List<double> evlelong = new List<double>();

                        ITerrainDynamicObject6 lujing = GlobalObjects.Instance.World3D.ProjectTree.GetObject(idsS) as ITerrainDynamicObject6;
                        if (lujing != null)
                        {
                            for (int i = 1; i < lujing.Waypoints.Count; i++)
                            {
                                double lenth = GlobalObjects.Instance.World3D.CoordServices.GetDistance(lujing.Waypoints.GetWaypoint(i).X, lujing.Waypoints.GetWaypoint(i).Y,
                                    lujing.Waypoints.GetWaypoint(i - 1).X, lujing.Waypoints.GetWaypoint(i - 1).Y);
                                longthforlu = longthforlu + lenth;
                                evlelong.Add(lenth);
                                allway.Add(new Vector2D(lujing.Waypoints.GetWaypoint(i).X - lujing.Waypoints.GetWaypoint(i - 1).X,
                                    lujing.Waypoints.GetWaypoint(i).Y - lujing.Waypoints.GetWaypoint(i - 1).Y));
                            }
                        }
                        double systemstl = Global.Instance.GlobalArmys[oneStep.ActionArmyID].MoveSpeed / 3.6 * oneStep.FightTimeLength * 60;
                        if (longthforlu > systemstl)
                        {
                            if(oneStep.FightTimeLength>0)
                           GlobalObjects.Instance.World3D.Window.ShowMessageBarText("按当前系统设定部队运行速度，该部队不能在指定时间内完成机动任务", MessageBarTextAlignment.MBT_LEFT,3);
                        }
                    }
                }
                double AN = Vector2D.AngleBetween(vc, new Vector2D(0, 1)) / Math.PI * 180;
                if (oneStep.ActionType == TK3DSim.Sim.ActionTypes.攻击)
                {
                    Fire(thetaget, thetagetrage);
                }

                else if (oneStep.ActionType == TK3DSim.Sim.ActionTypes.行进)
                {
                    //Run();
                }
                else if (oneStep.ActionType == (TK3DSim.Sim.ActionTypes.行进 | TK3DSim.Sim.ActionTypes.攻击))
                {
                    ///Run();

                    Fire(thetaget, thetagetrage);
                }
                //else if (oneStep.ActionType == (TK3DSim.Sim.ActionTypes.巡逻))
                //{

                //}
                //else if (oneStep.ActionType == (TK3DSim.Sim.ActionTypes.巡逻 | TK3DSim.Sim.ActionTypes.攻击))
                //{
                //    Fire(thetaget, thetagetrage);
                //}
            }

        //        Army army = Global.Instance.GlobalArmys[oneStep.ActionArmyID];
            //        int zbid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("装备");
            //        int armyid = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup(army.ArmyName, zbid);
            //        //army.ArmyPost
            //        int zbnum = army.ArmamentCount;

        //        string equid = Global.Instance.GlobalArmys[oneStep.ActionArmyID].EquipmentID;
            //        Armament equipmen = Global.Instance.GlobalArmaments[equid];
            //        Vector2D vc = new Vector2D();
            //        Vector2D vd = new Vector2D();
            //        if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.地标)
            //        {
            //            vc.X = oneStep.TargetLoaction.X - army.ArmyPost.X;
            //            vc.Y = oneStep.TargetLoaction.Y - army.ArmyPost.Y;
            //            vd = vc;
            //            vc.Normalize();
            //        }
            //        else if(oneStep.TargetKind == TK3DSim.Sim.TargetKinds.范围)
            //        {
            //            double cx =  (oneStep.TargetRange.LeftTopPoint.X+oneStep.TargetRange.RightBottom.X)/2;
            //            double cy =  (oneStep.TargetRange.LeftTopPoint.Y+oneStep.TargetRange.RightBottom.Y)/2;
            //            vc.X = cx - army.ArmyPost.X;
            //            vc.Y = cy - army.ArmyPost.Y;
            //            vd = vc;
            //            vc.Normalize();
            //        }
            //         double AN =   Vector2D.AngleBetween(vc,new Vector2D(0,1))/Math.PI*180;

        //        List<Location> allLocation = new List<Location>();
            //        if (army.Formation == Formations.三角形)
            //        {
            //            for (int i = 0; i < zbnum; i++)
            //            {
            //                if (i == 0)
            //                {
            //                     double x = army.ArmyPost.X;
            //                     double y = army.ArmyPost.Y;
            //                     ICoord2D aa=  GlobalObjects.Instance.World3D.CoordServices.MoveCoord( x,  y, vc.X * 250, vc.Y * 250);
            //                     Location ll = new Location();
            //                     ll.X = aa.X;
            //                     ll.Y = aa.Y;
            //                     allLocation.Add(ll);
            //                }

        //                if (i == 1)
            //                {

        //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, vc.X * 200, vc.Y * 200);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, -vc.Y * 50, vc.X * 50);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                } 

        //                if (i == 2)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, vc.X * 200, vc.Y * 200);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, vc.Y * 50, -vc.X * 50);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }

        //                if (i == 3)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 100, -vc.Y * 100);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, -vc.Y * 250, vc.X * 250);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }

        //                if (i == 4)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 150, -vc.Y * 150);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, -vc.Y * 300, vc.X * 300);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }

        //                if (i == 5)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 150, -vc.Y * 150);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, -vc.Y * 200, vc.X * 200);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }
            //                if (i == 6)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 100, -vc.Y * 100);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, vc.Y * 250, -vc.X * 250);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }
            //                if (i == 7)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 150, -vc.Y * 150);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, vc.Y * 300, -vc.X * 300);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }
            //                if (i == 8)
            //                {
            //                    double x = army.ArmyPost.X;
            //                    double y = army.ArmyPost.Y;
            //                    ICoord2D aa1 = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(x, y, -vc.X * 150, -vc.Y * 150);
            //                    ICoord2D aa = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(aa1.X, aa1.Y, vc.Y * 200, -vc.X * 200);
            //                    Location ll = new Location();
            //                    ll.X = aa.X;
            //                    ll.Y = aa.Y;
            //                    allLocation.Add(ll);
            //                }

        //            }
            //        }
            //        if(oneStep.ActionType == TK3DSim.Sim.ActionTypes.攻击)
            //        {
            //            foreach (Location lpoint in allLocation)
            //            {
            //                TerraExplorerX.IPosition6 point = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X, lpoint.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, -AN, 0, 0, 0);
            //                TerraExplorerX.IPosition6 pointt = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X + vc.X, lpoint.Y+vc.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0);
            //                TankClass tan = new TankClass(point, equipmen.ModelFileName, equipmen.ModelZoom, equipmen.ShowName, equipmen.ShowScal,
            //                    equipmen.FileToModelX,equipmen.FileToModelY,equipmen.FileToModelZ,
            //                    equipmen.TargteShowName, equipmen.TargteShowScal);
            //                //tan.ObjectRun();
            //                tan.ObjectFire();
            //                tan.FireTagete(pointt);
            //            }
            //        }

        //        else if (oneStep.ActionType == TK3DSim.Sim.ActionTypes.行进)
            //        {
            //            foreach (Location lpoint in allLocation)
            //            {
            //                TerraExplorerX.IPosition6 point = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X, lpoint.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, -AN, 0, 0, 0);
            //                TerraExplorerX.IPosition6 pointt = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X + vc.X, lpoint.Y + vc.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0);

        //                TankClass tan = new TankClass(point, pointt,equipmen.MidSpeed, equipmen.ModelFileName, equipmen.ModelZoom, equipmen.ShowName, equipmen.ShowScal,
            //                      equipmen.FileToModelX, equipmen.FileToModelY, equipmen.FileToModelZ,
            //                      equipmen.TargteShowName, equipmen.TargteShowScal);

        //                //////tan.ObjectRun();
            //                //tan.ObjectFire();
            //                //tan.FireTagete(pointt);
            //            }
            //            Location loc = new Location();
            //            loc.X = army.ArmyPost.X + vc.X;
            //            loc.Y = army.ArmyPost.Y + vc.Y;
            //            army.ArmyPost = loc;
            //        }
            //        else if (oneStep.ActionType == (TK3DSim.Sim.ActionTypes.行进 | TK3DSim.Sim.ActionTypes.攻击))
            //        {
            //            foreach (Location lpoint in allLocation)
            //            {
            //                TerraExplorerX.IPosition6 point = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X, lpoint.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, -AN, 0, 0, 0);
            //                TerraExplorerX.IPosition6 pointt = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X + vc.X, lpoint.Y + vc.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0);

        //                TankClass tan = new TankClass(point, pointt,equipmen.MidSpeed, equipmen.ModelFileName, equipmen.ModelZoom, equipmen.ShowName, equipmen.ShowScal,
            //                      equipmen.FileToModelX, equipmen.FileToModelY, equipmen.FileToModelZ,
            //                      equipmen.TargteShowName, equipmen.TargteShowScal);

        //                //////tan.ObjectRun();
            //                tan.ObjectFire();
            //                //tan.FireTagete(pointt);
            //            }
            //            army.ArmyPost.X = army.ArmyPost.X + vc.X;
            //            army.ArmyPost.Y = army.ArmyPost.Y + vc.Y;

        //        }
            //        else if (oneStep.ActionType == (TK3DSim.Sim.ActionTypes.巡逻))
            //        {

        //            foreach (Location lpoint in allLocation)
            //            {
            //                TerraExplorerX.IPosition6 point = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X, lpoint.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, -AN, 0, 0, 0);
            //                TerraExplorerX.IPosition6 pointt = GlobalObjects.Instance.World3D.Creator.CreatePosition(lpoint.X + vc.X, lpoint.Y + vc.Y, 0, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, 0, 0, 0, 0);

        //                TankClass tan = new TankClass(point, pointt, equipmen.MidSpeed, equipmen.ModelFileName, equipmen.ModelZoom, equipmen.ShowName, equipmen.ShowScal,
            //                      equipmen.FileToModelX, equipmen.FileToModelY, equipmen.FileToModelZ,
            //                      equipmen.TargteShowName, equipmen.TargteShowScal, oneStep.TargetRange);

        //                //////tan.ObjectRun();
            //                tan.ObjectFire();
            //                //tan.FireTagete(pointt);
            //            }
            //            army.ArmyPost.X = army.ArmyPost.X + vc.X;
            //            army.ArmyPost.Y = army.ArmyPost.Y + vc.Y;

        //        }
            //    }
            else if (oneStep.ActionKind == TK3DSim.Sim.ActionKinds.静态行动)
            {
                int id = GlobalObjects.Instance.World3D.ProjectTree.FindItem(oneStep.StaticActionLayerName);
                if (id > 0)
                    GlobalObjects.Instance.World3D.Navigate.FlyTo(id, TerraExplorerX.ActionCode.AC_PLAY);
            }
            else if (oneStep.ActionKind == TK3DSim.Sim.ActionKinds.视频)
            {
                FormPlayer newplayer = new FormPlayer();
                newplayer.Show(Mainform.PlayerForm);
                newplayer.loadfile(oneStep.StaticActionLayerName);
            }
            //    //Global.Instance.GlobalArmaments[equid].Name
        }

        void findallarmy(Army arm)
        {
            if (arm.HaveSubUnit)
            {
                foreach (object armyy in Global.Instance.GlobalArmys.GetSons(arm.ID))
                {
                    Army onea = armyy as Army;
                    if (onea != null)
                    {
                        allarmys.Add(onea);
                        findallarmy(onea);
                    }
                }
            }
        }

        void Fire(Location thetaget, Location thetagetrage)
        {

            Random a = new Random(1);
            Random d = new Random(1);
            Random b = new Random(0);
            Random c = new Random(0);

            foreach (Army lpoint in allarmys)
            {

                int fireid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + lpoint.ArmyName + "\\开火");
                int tegetid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("部队\\" + lpoint.ArmyName + "\\目标");
                GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(fireid, true);
                int objid12 = GlobalObjects.Instance.World3D.ProjectTree.GetNextItem(tegetid, ItemCode.CHILD);
                while (objid12 > 0)
                {
                    ITerrainImageLabel6 imgfire = GlobalObjects.Instance.World3D.ProjectTree.GetObject(objid12) as ITerrainImageLabel6;
                    if (imgfire != null)
                    {
                        if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.地标)
                        {
                            int i = a.Next(4);
                            int j = d.Next(4);
                            int bX = b.Next(100);
                            int bY = c.Next(100);
                            Location pois = new Location();
                            ICoord2D OADD = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(oneStep.TargetLoaction.X, oneStep.TargetLoaction.Y, ((-1) ^ i) * bX, ((-1) ^ j) * bY);
                            pois.X = OADD.X;
                            pois.Y = OADD.Y;
                            GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(objid12, true);
                            imgfire.Position.X = pois.X;
                            imgfire.Position.Y = pois.Y;

                        }

                        else if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.范围)
                        {
                            int i = a.Next(4);
                            int j = d.Next(4);
                            int bX = b.Next((int)thetagetrage.X);
                            int bY = c.Next((int)thetagetrage.Y);
                            Location pois = new Location();
                            ICoord2D OADD = GlobalObjects.Instance.World3D.CoordServices.MoveCoord(thetaget.X, thetaget.Y, ((-1) ^ i) * bX, ((-1) ^ j) * bY);
                            pois.X = OADD.X;
                            pois.Y = OADD.Y;
                            GlobalObjects.Instance.World3D.ProjectTree.SetVisibility(objid12, true);
                            imgfire.Position.X = pois.X;

                            imgfire.Position.Y = pois.Y;
                        }
                    }
                    objid12 = GlobalObjects.Instance.World3D.ProjectTree.GetNextItem(objid12, ItemCode.NEXT);
                }

                if (Global.Instance.GlobalArmaments[lpoint.EquipmentID].TargteShowName == "线")
                {
                    if (oneStep.TargetKind == TK3DSim.Sim.TargetKinds.地标)
                    {

                    }

                }
            }
        }
        private void armmrunbyline(int armm, ITerrainDynamicObject6 lujing, Location stat)
        {
            int basid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("路径");
            int ids;
            int iiisid = GlobalObjects.Instance.World3D.ProjectTree.FindItem("路径\\" + oneStep.AdditionalInfo + "组");
            if (iiisid > 0)
            {
                ids = iiisid;
            }
            else
            {
                ids = GlobalObjects.Instance.World3D.ProjectTree.CreateGroup(oneStep.AdditionalInfo + "组", basid);
            }
            Location point = new Location();
            ITerrainModel6 imodel = GlobalObjects.Instance.World3D.ProjectTree.GetObject(armm) as ITerrainModel6;
            ITerrainLabel6 ileble = null;
            if (imodel == null)
            {
                ileble = GlobalObjects.Instance.World3D.ProjectTree.GetObject(armm) as ITerrainLabel6;

                if (ileble != null)
                {
                    point.X = ileble.Position.X;
                    point.Y = ileble.Position.Y;
                }
            }
            else
            {

                point.X = imodel.Position.X;
                point.Y = imodel.Position.Y;
            }

            Vector2D VI = new Vector2D(point.X - stat.X, point.Y - stat.Y);
            double[] vv = new double[] {
            };


            ITerrainDynamicObject6 pdyna = GlobalObjects.Instance.World3D.Creator.CreateDynamicObject(vv, TerraExplorerX.DynamicMotionStyle.MOTION_GROUND_VEHICLE, TerraExplorerX.DynamicObjectType.DYNAMIC_VIRTUAL, "路径", 1, TerraExplorerX.AltitudeTypeCode.ATC_ON_TERRAIN, ids, "");
            for (int jj = 0; jj < lujing.Waypoints.Count; jj++)
            {
                IRouteWaypoint6 onepoint = lujing.Waypoints.GetWaypoint(jj);
                onepoint.X = onepoint.X + VI.X;
                onepoint.Y = onepoint.Y + VI.Y;
                pdyna.Waypoints.AddWaypoint(onepoint);
            }
            if (imodel == null)
            {
                if (ileble != null)
                {
                    ileble.Attachment.AttachTo(pdyna.ID, 00, 0, 0);
                }
            }
            else
            {
                imodel.Attachment.AttachTo(pdyna.ID, 00, 0, 0);
            }
        }
        void returnagein()
        {

        }

    }
}
